<template>
  <div class="container">
    <div class="header">
      <el-input class="input" v-model="brandId" placeholder="请输入品牌商ID" clearable></el-input>
      <el-input class="input" v-model="brandName" placeholder="请输入品牌商名称" clearable></el-input>
      <el-button class="btn" type="primary" icon="el-icon-search" @click="getData()">查找</el-button>
      <el-button class="btn" type="primary" icon="el-icon-edit" @click="addBtn">添加</el-button>
      <el-button class="btn" type="primary" icon="el-icon-download">导出</el-button>
    </div>

    <div class="table-box">
      <el-table :data="tableData" border style="width: 100%;">
        <el-table-column prop="id" label="品牌商ID" width="140" align="center">
        </el-table-column>
        <el-table-column prop="name" label="品牌商名称" width="140" align="center">
        </el-table-column>
        <el-table-column prop="picUrl" label="品牌商图片" align="center" width="140">
          <template slot-scope="scope">
            <img :src="scope.row.picUrl" alt="" width="90">
          </template>
        </el-table-column>
        <el-table-column prop="desc" label="介绍" align="center">
        </el-table-column>
        <el-table-column prop="floorPrice" label="底价" align="center" width="120">
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="180">
          <template slot-scope="scope">
            <el-button @click="edit(scope.row)" type="primary" size="small">编辑</el-button>
            <el-button type="danger" size="small" @click="dele(scope.row)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
    </div>

    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" background @current-change="handleCurrentChange"
        :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 嵌套表单的对话框 -->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="*品牌商名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="介绍" :label-width="formLabelWidth">
          <el-input v-model="form.desc" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌商图片" :label-width="formLabelWidth">
          <el-upload action="#" list-type="picture-card" :auto-upload="false">
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{file}">
              <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
              <span class="el-upload-list__item-actions">
                <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                  <i class="el-icon-zoom-in"></i>
                </span>
                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)">
                  <i class="el-icon-download"></i>
                </span>
                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                  <i class="el-icon-delete"></i>
                </span>
              </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="底价" :label-width="formLabelWidth">
          <el-input v-model="form.floorPrice" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addItem">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>


import moment from "moment";
import { getBrandData, createNewBrand, updateBrand, deleteBrand } from '../../api/mall-manage'


export default {
  name: 'brand',
  data() {
    return {
      brandName: '',//
      brandId: '',//
      tableData: [],//渲染的表格数据
      currentPage: 1,//当前页
      limit: 20,//每页显示的数目
      total: 0,//表格数据总数
      isEditing: false,
      dialogFormVisible: false,

      form: {
        name: '',
        desc: '',
        floorPrice: '',
        delivery: false,
        type: [],
        resource: '',
        desc: '',
        id: 0,
        
      },
      formLabelWidth: '120px',

      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false
    }
  },

  methods: {
    // 调接口获取数据
    getData() {
      getBrandData(this.currentPage, this.limit, this.brandId, this.brandName).then(res => {
        let { data, errno } = res;
        if (errno == 0) {
          this.tableData.splice(0, this.tableData.length)
          this.total = data.total
          // data.items.forEach(item => {
          //   this.tableData.push({
          //     id: item.id,
          //     name: item.name,
          //     picUrl: item.picUrl,
          //     desc: item.desc,
          //     floorPrice: item.floorPrice,
          //   })
          // })
          this.tableData.push(...data.items)
        }
      })
    },



    // 每页显示条目数改变事件
    handleSizeChange(e) {
      // console.log(e);
      this.limit = e
      this.getData()
    },

    // 当前页改变事件
    handleCurrentChange(e) {
      // console.log(e);
      this.currentPage = e
      this.getData()

    },

    // 添加按钮事件
    addBtn() {
      this.isEditing = false;
      this.dialogFormVisible = true
      this.form.desc = '';
      this.form.name = '';
      this.form.floorPrice = '';
      this.dialogImageUrl = '';
    },


    // 编辑按钮事件
    edit(e) {
      console.log(e);
      this.isEditing = true;
      this.dialogFormVisible = true;
      this.form.name = e.name;
      this.form.desc = e.desc;
      this.form.floorPrice = e.floorPrice;
      this.form.id=e.id
    },

    // 添加新品牌商确定按钮
    addItem() {
      this.dialogFormVisible = false
      if (this.isEditing) {
        // 进到这里代表是编辑
        updateBrand({
          addTime: moment(new Date()).format("YYYY-MM-DD HH:ss:mm"),
          desc: this.form.desc,
          floorPrice: this.form.floorPrice,
          name: this.form.name,
          deleted: false,
          id: this.form.id,
          picUrl: this.dialogImageUrl,
          sortOrder: 50,
          updateTime: moment(new Date()).format("YYYY-MM-DD HH:ss:mm"),
        }).then(res => {
          let { data, errno } = res;
          if (errno == 0) {
            let index = this.tableData.findIndex(item => {
              return item.id==data.id
            })
            console.log(index);
            this.tableData[index]=data
          }
        }).then(() => {
          this.getData()
        })
      } else {
        // 进到这里代表创建

        createNewBrand({
          desc: this.form.desc,
          floorPrice: this.form.floorPrice,
          name: this.form.name,
        }).then(res => {
          if (res.errno == 0) {
            this.tableData.push(res.data);
            this.$message({
              message: '添加成功',
              type: 'success'
            });
          } else {
            this.$message.error(res.errmsg);
          }
        }).then(() => {
          this.getData()
        })
      }
    },

    // 删除按钮
    dele(e) {
      let id = e.id;
      let index = this.tableData.findIndex(item => {
        return item.id==id
      })
      let item = this.tableData[index]
      deleteBrand(item).then(res => {
        if (res.errno == 0) {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
        }
      }).then(() => {
        this.getData()
      })
      
    },

    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },






  },
  mounted() {
    this.getData()
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.container {
  padding: 20px;

  .header {
    .input {
      width: 200px;
      margin-right: 5px;
    }
  }


  .table-box {
    width: 100%;
    margin-top: 30px;
  }

  .pagination {
    width: 100%;
    margin: 50px auto;
    padding: 0 30px;
  }
}
</style>